/*
 * execCommand 사용
 */
	function iBold(){
		document.execCommand('bold', false, null); 
	}
	function iUnderline(){
		document.execCommand('underline', false, null);
	}
	function iStrikeThrough(){
		rte.document.execCommand('strikeThrough', false, null);
	}
	function iStrikeThrough(){
		document.execCommand('strikeThrough', false, null);
	}
	function iItalic(){
		document.execCommand('italic', false, null);
	}
	function changefontsize(){
		var choose = document.getElementById("fontsize").value;
		document.execCommand('fontSize', false, choose);
	}
	function iBackColor(color){
		document.execCommand('backColor', false, color);
		document.execCommand('hiliteColor', false, color);
	}
	function iCreateLink(){
		var szURL = prompt("Enter a URL:", "http://");
		document.execCommand('CreateLink', false, szURL);
	}
	function iCopy(){
		document.execCommand('copy', false, null);
	}
	function iPaste(){
		document.execCommand('paste', false, null);
	}
	function iCut(){
		document.execCommand('cut', false, null);
	}
	function iDelete(){
		document.execCommand('delete', false, null);
	}
	
	function changefont() {
		var choose = document.getElementById("fontfamily").value;
		document.execCommand('fontName', false, choose);
	
	}
	function iFontColor(color){
		document.execCommand('foreColor', false, color);
	}
	function iIndent(){
		document.execCommand('indent', false, null);
	}
	function iOutdent(){
		document.execCommand('outdent', false, null);
	}
	function iHorizontalRule(){
		document.execCommand('InsertHorizontalRule', false, null); // ?????
	}
	function iImage(){
		document.execCommand('insertImage', false, './image/dog1.jpg');
	}
	function iItalic(){
		document.execCommand('italic', false, null);
	}
	function iJustifyLeft(){
		document.execCommand('JustifyLeft', false, null);
	}
	function iJustifyRight(){
		document.execCommand('JustifyRight', false, null);
	}
	function iJustifyCenter(){
		document.execCommand('JustifyCenter', false, null);
	}
	function justifyFull(){
		document.execCommand('JustifyFull', false, null); // 양쪽정렬. 안됨. ??
	}
	function iInsertOrderedList(){
		document.execCommand('InsertOrderedList', false, null);
	}
	function iInsertUnorderedList(){
		document.execCommand('insertUnorderedList', false, null);
	}
	function iInsertParagraph(){
		document.execCommand('insertParagraph', false, null);
	}
	function iSuperScript(){
		document.execCommand('superscript', false, null); // 위 첨자
	}
	function iSubscript(){
		document.execCommand('subscript', false, null);
	}
	function undo(){
		document.execCommand('undo', false, null); 
	}
	function redo(){
		document.execCommand('redo', false, null); 
	}
	function increaseFontSize(){
		document.execCommand('increaseFontSize', false, null); // IE에서는 지원안함. 크롬도 안됨. ??
	}
	function decreaseFontSize(){
		document.execCommand('decreaseFontSize', false, null); // IE에서는 지원안함. 크롬도 안됨. ??
	}

/*
 * 메모 삽입
 */
	function createMemo(){
	
	//	event.preventDefault();
		var selection= window.getSelection().getRangeAt(0);
		var selectedText = selection.extractContents();
		var span= document.createElement("span");
	
		span.appendChild(selectedText);
		selection.insertNode(span);
	
		var maindiv = document.getElementById('editorContainer');
	
	
	
		if(span.innerText == "") { // 아무 입력 없는 경우
			alert('텍스트를 선택해주세요.');
		}else {
	
			if(!document.getElementById('commentDiv')) {       //CommentDiv 영역 처음 생성 시 한번만 만들어줌
				var commentDiv = document.createElement('div');
				commentDiv.id = "commentDiv";
				commentDiv.className = "commentDiv";
				maindiv.appendChild(commentDiv);
			}
			var commentBox = document.createElement("div");    //CommentBox 생성
			commentBox.className = "commentbox";
			document.getElementById('commentDiv').appendChild(commentBox);
	
			var div1 = document.createElement("div");
			div1.innerHTML = '"'+span.innerText+'"'+"에 대한 메모";
			var deleteBtn = document.createElement("button");
			deleteBtn.innerText = "x";
			deleteBtn.onclick = function(e) {                  //CommentBox 삭제 버튼
	
				var targetComment = e.target.parentNode;
				targetComment.parentNode.removeChild(targetComment);
	
			};
			var inputDiv = document.createElement("div");
			var input1 = document.createElement("input");
			input1.type = "text";
			var input2 = document.createElement("input");
			input2.type = "button";
			input2.value = "memo";
			input2.onclick = function() {                      //CommentBox 메모 추가 이벤트
				var text = document.createTextNode(input1.value);
				input1.value = '';
	
				var x = document.createElement("div");
				x.innerHTML=getTodayDate();                    //날짜 입력
				commentBox.appendChild(text);
				commentBox.appendChild(x);
			};
			inputDiv.appendChild(input1);
			inputDiv.appendChild(input2);
	
			div1.appendChild(deleteBtn);
			commentBox.appendChild(div1);
			commentBox.appendChild(inputDiv);
		}
	}
	
/*
 *  현재 연, 월, 일, 시간 구하는 함수
 */
	function getTodayDate() {
	
		var today = new Date();
		var month = today.getMonth()+1;
		var day = today.getDate();
		var year = today.getFullYear();
		var hours = today.getHours();
		var minute = today.getMinutes();
		var second = today.getSeconds();
	
		return year + ". " + month + ". " + day+ " "+hours+":"+minute+":"+second;
	}
	
/*
 *  줄 간격
 */
	function iLineHeight() {
	
		var content = prompt('줄 간격을 입력하시요.','');
		var selection= window.getSelection().getRangeAt(0);
		var selectedText = selection.extractContents();
		var span= document.createElement("span");
	
		span.style.lineHeight = content;
		span.style.verticalAlign = "top";
		span.appendChild(selectedText);
		selection.insertNode(span);
	
	}
/*
 *  글자 간격
 */
	function iLetterSpace() {
	
		var content = prompt('글자 간격을 입력하시요.','');
		var selection= window.getSelection().getRangeAt(0);
		var selectedText = selection.extractContents();
		var span= document.createElement("span");
	
		span.style.letterSpacing = content+'px';
		span.appendChild(selectedText);
		selection.insertNode(span);
	
	}
/*
 *  우클릭
 */
	function RightMenu(event) {
	
		var btn = event.button;
		if(btn == 2) {
			ShowMenu('contextMenu',event);    //오른쪽 클릭
			event.preventDefault();
	
		}else {
			HideMenu('contextMenu');          //왼쪽 클릭
		}
	
	}
	function ShowMenu(control, e) {
	
		var posx = e.clientX +window.pageXOffset +'px';   //마우스 포인터  Left Position
		var posy = e.clientY + window.pageYOffset + 'px'; //마우스 포인터  Top Position
		document.getElementById(control).style.position = 'absolute';
		document.getElementById(control).style.display = 'inline';
		document.getElementById(control).style.left = posx;
		document.getElementById(control).style.top = posy;
	
	
	}
	function HideMenu(control) {
	
		document.getElementById(control).style.display = 'none'; 
	}
	
	function selectText() {
		var selectionText = "";
		if (document.getSelection) {
			selectionText = document.getSelection();
		} else if (document.selection) {
			selectionText = document.selection.createRange().text;
		}
		return selectionText;
	}
/*
 *  Text Style Status
 */	
	
	setInterval( function() {
		var isBold = document.queryCommandValue("Bold");
		var isItalic = document.queryCommandValue("Italic");
		var isUnderLine = document.queryCommandValue("UnderLine");
		var isStrikeThrough = document.queryCommandValue("StrikeThrough");
		var isLink = document.queryCommandValue("HyperLink");
	//	var curFontSize = document.queryCommandValue("fontSize");
	//	var curFontName = document.queryCommandValue("fontName");
		var isLeft = document.queryCommandValue("JustifyLeft");
		var isCenter = document.queryCommandValue("JustifyCenter");
		var isRight = document.queryCommandValue("JustifyRight");
		var isIndent = document.queryCommandValue("indent");
		var isOutdent = document.queryCommandValue("outdent");
		var isHorizontal = document.queryCommandValue("horizontal");
		var isNumberlist = document.queryCommandValue("InsertOrderedList");
		var isCirclelist = document.queryCommandValue("InsertUnOrderedList");
		var isSuperscript = document.queryCommandValue("superscript");
		var isSubscript = document.queryCommandValue("subscript");
	
		// Bold 체크
		if (isBold === 'true') {
			$("#bold").css({
				"border" : "2px solid black",
				"border-radius" : "5px"
			});
		} else {
			$("#bold").css({
				"border" : "0px solid black",
				"border-radius" : "0px"
			});
		}
	
		//Italic 체크
		if (isItalic === 'true') {
			$("#italic").css({
				"border" : "2px solid black",
				"border-radius" : "5px"
			});
		} else {
			$("#italic").css({
				"border" : "0px solid black",
				"border-radius" : "0px"
			});
		}
	
		//Underline 체크
		if (isUnderLine === 'true') {
			$("#underline").css({
				"border" : "2px solid black",
				"border-radius" : "5px"
			});
		} else {
			$("#underline").css({
				"border" : "0px solid black",
				"border-radius" : "0px"
			});
		}
	
		//취소선 체크
		if (isStrikeThrough === 'true') {
			$("#strikethrough").css({
				"border" : "2px solid black",
				"border-radius" : "5px"
			});
		} else {
			$("#strikethrough").css({
				"border" : "0px solid black",
				"border-radius" : "0px"
			});
		}
	
		//링크 체크
		if (isLink === 'true') {
			$("#link").css({
				"border" : "2px solid black",
				"border-radius" : "5px"
			});
		} else {
			$("#link").css({
				"border" : "0px solid black",
				"border-radius" : "0px"
			});
		}
	
	//	//글자 크기 체크
	//	if (curFontSize) {
	//	console.log("현재 글자 크기 : " + curFontSize);
	//	$("select[name='fontSize']").val(curFontSize);
	//	} else {
	//	$("select[name='fontSize']").prop('selectedIndex', 0);
	//	}
	
	//	//폰트 체크
	//	if(curFontName) {
	//	console.log("현재 글자 종류 : " + curFontName);
	//	$("select[name='fontName']").val(curFontName);
	//	} else {
	//	$("select[name='fontName']").prop('selectedIndex', 0);
	//	}
	
		//왼쪽정렬 체크
		if (isLeft === 'true') {
			$("#left").css({
				"border" : "2px solid black",
				"border-radius" : "5px"
			});
		} else {
			$("#left").css({
				"border" : "0px solid black",
				"border-radius" : "0px"
			});
		}
	
		//가운데 정렬 체크
		if (isCenter === 'true') {
			$("#center").css({
				"border" : "2px solid black",
				"border-radius" : "5px"
			});
		} else {
			$("#center").css({
				"border" : "0px solid black",
				"border-radius" : "0px"
			});
		}
	
		//오른쪽 정렬 체크
		if (isRight === 'true') {
			$("#right").css({
				"border" : "2px solid black",
				"border-radius" : "5px"
			});
		} else {
			$("#right").css({
				"border" : "0px solid black",
				"border-radius" : "0px"
			});
		};
	
		//들여쓰기 체크
		if (isIndent === 'true') {
			$("#indent").css({
				"border" : "2px solid black",
				"border-radius" : "5px"
			});
		} else {
			$("#indent").css({
				"border" : "0px solid black",
				"border-radius" : "0px"
			});
		};
	
		//내어쓰기 체크
		if (isOutdent === 'true') {
			$("#outdent").css({
				"border" : "2px solid black",
				"border-radius" : "5px"
			});
		} else {
			$("#outdent").css({
				"border" : "0px solid black",
				"border-radius" : "0px"
			});
		};
	
		//수평선 체크
		if (isHorizontal === 'true') {
			$("#horizontal").css({
				"border" : "2px solid black",
				"border-radius" : "5px"
			});
		} else {
			$("#horizontal").css({
				"border" : "0px solid black",
				"border-radius" : "0px"
			});
		};
	
		//문단번호(숫자) 체크
		if (isNumberlist === 'true') {
			$("#numberlist").css({
				"border" : "2px solid black",
				"border-radius" : "5px"
			});
		} else {
			$("#numberlist").css({
				"border" : "0px solid black",
				"border-radius" : "0px"
			});
		};
	
		//문단번호(도형) 체크
		if (isCirclelist === 'true') {
			$("#circlelist").css({
				"border" : "2px solid black",
				"border-radius" : "5px"
			});
		} else {
			$("#circlelist").css({
				"border" : "0px solid black",
				"border-radius" : "0px"
			});
		};
	
		//위첨자 체크
		if (isSuperscript === 'true') {
			$("#superscript").css({
				"border" : "2px solid black",
				"border-radius" : "5px"
			});
		} else {
			$("#superscript").css({
				"border" : "0px solid black",
				"border-radius" : "0px"
			});
		};
	
		//아래 첨자 체크
		if (isSubscript === 'true') {
			$("#subscript").css({
				"border" : "2px solid black",
				"border-radius" : "5px"
			});
		} else {
			$("#subscript").css({
				"border" : "0px solid black",
				"border-radius" : "0px"
			});
		}
	}, 100);
	
/*
 *  찾기, 찾아서 바꾸기
 */	
	
	function FindNext () {
		var str = document.getElementById ("findField").value;
		if (str == "") {
			alert ("텍스트를 선택해주세요!");
			return;
		}
	
	
		var found = false;
		if (window.find) {        // Firefox, Google Chrome, Safari
	
			found = window.find (str);
		}
		else {
			if (document.selection && document.selection.createRange) { // Internet Explorer, Opera before version 10.5
				var textRange = document.selection.createRange ();
				if (textRange.findText) {   
	
					if (textRange.text.length > 0) {
						textRange.collapse (true);
						textRange.move ("character", 1);
					}
	
					found = textRange.findText (str);
					
					if (found) {
						textRange.select ();
					}
				}
			}
		}
	
	
		if (!found) {
			alert ("다음 텍스트는 없습니다.:\n" + str);
		}
	
	}	
	
	function ChangeText() {
	
		var str = document.getElementById("changeField").value;
	
		var selection= window.getSelection().getRangeAt(0);
		var selectedText = selection.extractContents();
		var span= document.createElement("span");
	
	
		span.appendChild(selectedText);
		selection.insertNode(span);
	
		span.innerText = str;
	
	
	}
